library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(p8105.datasets)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
We are going to look at “The Instacart Online Grocery Shopping Dataset 2017”, we are only interested in items ordered in produce, snack, and beverage department on weekdays.
data("instacart")
instacart = instacart |>
select(add_to_cart_order, reordered, order_number, department, aisle, days_since_prior_order, product_name, order_dow, order_hour_of_day) |>
filter(department %in% c("produce", "snacks", "beverages")) |>
filter(order_dow %in% c("1", "2", "3", "4", "5"))
The bar plot shows the number of items ordered in each aisle in produce, beverages, and snacks department.
instacart |>
group_by(department) |>
count(aisle) |>
mutate(aisle = fct_reorder(aisle, n),
text_label = "Department", department) |>
plot_ly(
x = ~aisle, y = ~n, type = "bar", color = ~department, text = ~text_label, alpha = 0.5)
The scatter plot shows the tendency of reorder in different hours of the day.
instacart |>
group_by(order_hour_of_day) |>
mutate(reordered=mean(reordered))|>
plot_ly(x = ~order_hour_of_day, y = ~reordered, type = "scatter", mode = "markers")
The box plot compares order in which each product was added to cart in different department.
instacart |>
mutate(department = fct_reorder(department, add_to_cart_order)) |>
plot_ly(y = ~add_to_cart_order, color = ~department, type = "box")